/**
 * Copyright 2010 Pavel Daniel Alexandru PFA
 * 
 * Licensed under the terms of the GNU Lesser General Public License version 3 
 * as published by the Free Software Foundation. You may obtain a copy of the
 * License at: http://www.gnu.org/copyleft/lesser.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations 
 * under the License. 
 */
package com.kobol.webclient.client.data;

import com.google.gwt.i18n.client.LocaleInfo;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.types.DSDataFormat;
import com.smartgwt.client.types.FieldType;

/**
 * @author Pavel Alexandru
 * 
 */
public class SettingsDataSource extends DataSource {

    private static final String DATA_SOURCE = "settingsDS";
    private static final String ICON = "icon";
    private static final String ICON_DISPLAY_NAME = "Icon";
    private static final String NAME = "name";
    private static final String NAME_DISPLAY_NAME = "Name";
    private static final String RECORD_XPATH = "/list/record";
    private static final String URL_PREFIX = "datasource/data/settingsdatasource";
    private static final String URL_SUFFIX = ".xml";

    private static SettingsDataSource instance = null;

    public static SettingsDataSource getInstance() {
	if (instance == null) {
	    instance = new SettingsDataSource(DATA_SOURCE);
	}
	return instance;
    }

    public SettingsDataSource(String id) {
	setID(id);
	setDataFormat(DSDataFormat.XML);
	setRecordXPath(RECORD_XPATH);
	DataSourceField iconField = new DataSourceField(ICON, FieldType.TEXT, ICON_DISPLAY_NAME);
	DataSourceField nameField = new DataSourceField(NAME, FieldType.TEXT, NAME_DISPLAY_NAME);
	setFields(iconField, nameField);

	String url = URL_PREFIX;
	LocaleInfo localeInfo = LocaleInfo.getCurrentLocale();
	String localeName = localeInfo.getLocaleName();

	if (localeName.length() > 0) {
	    url = url + "_" + localeName;
	}

	url = url + URL_SUFFIX;
	setDataURL(url);
    }
}
